﻿using IService.Blog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 类别管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class SortController : ControllerBase
    {
        readonly ILogger<SortController> logger;
        readonly ISortService sortService;
        public SortController(ILogger<SortController> _logger, ISortService _sortService)
        {
            logger = _logger;
            sortService = _sortService;
        }

        /// <summary>
        /// 查询所有类别（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有类别")]
        public async Task<MessageModel<PageInfoModel<BSort>>> GetAllToPage(int offset=1, int limit=15)
        {
            try
            {
                return await sortService.GetAllToPage(offset, limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<PageInfoModel<BSort>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询所有类别
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有类别")]
        public async Task<MessageModel<List<BSort>>> GetAll()
        {
            try
            {
                return await sortService.GetAll();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BSort>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询类别
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询类别")]
        public async Task<MessageModel<BSort>> GetById(int id)
        {
            try
            {
                return await sortService.GetById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<BSort>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 添加类别
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加类别")]
        public async Task<MessageModel<string>> Post([FromBody] BSort parameter)
        {
            try
            {
                return await sortService.Post(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改类别")]
        public async Task<MessageModel<string>> Put([FromBody] BSort parameter)
        {
            try
            {
                return await sortService.Put(parameter);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 删除类别
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除类别")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await sortService.DelById(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


    }
}
